package com.crm.service.impl.customerManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.customMapper.customerManagerCustomMapper.CustomerContactPersonCustomMapper;
import com.crm.dao.mapper.customerManager.CustomerContactPersonMapper;
import com.crm.dao.mapper.customerManager.CustomerReturnMapper;
import com.crm.model.entity.customerManager.CustomerContactPerson;
import com.crm.model.entity.customerManager.CustomerContactPersonExample;
import com.crm.model.entity.customerManager.CustomerReturn;
import com.crm.model.entity.customerManager.CustomerReturnExample;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.contactManager.ContactRepeatVO;
import com.crm.service.customerManager.CustomerContactPersonDbService;
import com.crm.service.customerManager.CustomerReturnDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ Author ：pym
 * @ Date   ：2021/1/22 16:26
 */
@Service
public class CustomerContactPersonDbServiceImpl implements CustomerContactPersonDbService {
    @Autowired
    CustomerContactPersonMapper customerContactPersonMapper;

    @Autowired
    CustomerContactPersonCustomMapper customerContactPersonCustomMapper;

    /**
     * 删除客户下联系人
     * @param customerContactPerson 客户联系人实体
     * @return
     */
    @Override
    public Boolean deleteCustomerContactPersonById(CustomerContactPerson customerContactPerson) {
        return customerContactPersonMapper.updateByPrimaryKeySelective(customerContactPerson) > 0;
    }

    /**
     * 根据客户id、联系人名称和联系方式判重，是否插入客户联系人
     * @Author pym
     * @Date  2021/3/4 14:01
     * @param customerId --客户id
     * @param contactName --联系人姓名
     * @param contactInformation --联系方式
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public CustomerContactPerson isInsertCustomerContactPerson(String customerId, String contactName, String contactInformation) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Boolean result = false;
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andContactNameEqualTo(contactName);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andCompanyIdEqualTo(userAccount.getCompanyId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerContactPerson> customerContactPeoples = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        if(customerContactPeoples.size()>0){
            return customerContactPeoples.get(0);
        }else {
            return null;
        }
    }

    @Override
    public List<CustomerContactPerson> selectCustomerContactPersons(String customerId, String contactName) {
        return customerContactPersonCustomMapper.selectCustomerContactPersons(customerId, contactName);
    }

    /**
     * 通过客户id和联系人名称名称和联系方式来修改客户下联系人的联系方式成为主联系人
     * @param customerId 客户id
     * @param contactName 联系人名称
     * @param contactInformation 联系方式
     * @return
     */
    @Override
    public CustomerContactPerson getCustomerIdAndContactNameAndLxfs(String customerId, String contactName, String contactInformation) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andContactNameEqualTo(contactName);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andCompanyIdEqualTo(userAccount.getCompanyId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerContactPerson> list = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        if(list.size() == 0) {
            return new CustomerContactPerson();
        }else{
            return list.get(0);
        }
    }

    /**
     * 修改客户下联系人表
     * @param customerContactPerson 客户下联系人表实体
     * @return
     */
    @Override
    public Boolean updateCustomerContactPerson(CustomerContactPerson customerContactPerson) {
        Boolean result = false;
        int num = customerContactPersonMapper.updateByPrimaryKeySelective(customerContactPerson);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 通过客户id和联系人类型 主联系人-1   从联系人-2  查询一个客户下只能有一个主联系人-->1 的联系人
     * @param customerId 客户id
     * @param contactState  联系人类型 主联系人-1   从联系人-2
     * @return
     */
    @Override
    public CustomerContactPerson getCustomerIdAndContactState(String customerId, Integer contactState) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andContactStateEqualTo(contactState);
        //废止状态  1启动 2废止
        criteria.andAbolishStateEqualTo(1);
        criteria.andCompanyIdEqualTo(userAccount.getCompanyId());
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerContactPerson> list = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        if(list.size() == 0) {
            return new CustomerContactPerson();
        }else{
            return list.get(0);
        }
    }

    /**
     * 根据客户ID查询客户下的联系人
     * @Author pym
     * @Date  2021/4/14 15:26
     * @param customerId
     * @return {@link {java.util.List<com.crm.model.entity.customerManager.CustomerContactPerson>}}
     */
    @Override
    public List<CustomerContactPerson> selectCustomerContactPersonList(String customerId) {
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        return customerContactPersonMapper.selectByExample(customerContactPersonExample);
    }

    /**
     * 通过客户id和联系人姓名和联系方式和分公司id查询客户下联系人有没有数据
     * @param customerId 客户id
     * @param contactName 联系人姓名
     * @param contactInformation 联系方式
     * @param companyId 公司id
     * @return
     */
    @Override
    public CustomerContactPerson selectCustomerContactPerson(String customerId, String contactName, String contactInformation, String companyId) {
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andContactNameEqualTo(contactName);
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CustomerContactPerson> list = customerContactPersonMapper.selectByExample(customerContactPersonExample);
        if(list != null && list.size() > 0){
            return list.get(0);
        }
        return new CustomerContactPerson();
    }

    /**
     * 通过分公司id进行去重
     * @param companyId 公司id
     * @return
     */
    @Override
    public List<ContactRepeatVO> selectContactrepeat(String companyId) {
        return customerContactPersonCustomMapper.selectContactrepeat(companyId);
    }

    /**
     * (客服部)--根据客户ID修改客户联系人
     * @author yincuimei
     * @return
     */
    @Override
    public Boolean updateCustomerPersonByCustomerId(CustomerContactPerson customerContactPerson,String customerId) {
        CustomerContactPersonExample customerContactPersonExample = new CustomerContactPersonExample();
        CustomerContactPersonExample.Criteria criteria = customerContactPersonExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        return customerContactPersonMapper.updateByExampleSelective(customerContactPerson,customerContactPersonExample) > 0;
    }

    /**
     * 根据客户ID更新客户联系人分公司ID
     * @Author pym
     * @Date  2021/8/20 16:12
     * @param customerId     -客户ID
     * @param companyId      -公司ID
     * @param accountId      -更新人ID
     * @param updateTm       -更新时间
     * @return {@link {java.lang.Boolean}}
     */
    @Override
    public Boolean updateCustomerContactPersonByCustomerId(String customerId, String companyId, String accountId, Date updateTm) {
        return customerContactPersonCustomMapper.updateCustomerContactPersonsByCustomerId(customerId, companyId, accountId, updateTm)  > 0;
    }
}
